﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;
using System.Text.RegularExpressions;
using QLTT.Properties;

namespace QLTT.ViewModels
{
    class LoginViewModel : WorkspaceViewModel, System.ComponentModel.IDataErrorInfo
    {
        private icanDataContext _dataContext;
        private DelegateCommand _loginCommand;
        public LoginViewModel()
        {
            _dataContext = new icanDataContext();
            base.DisplayName = Properties.Resources.Base_DisplayName_Login;
        }

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new DelegateCommand(Login);
                }
                return _loginCommand;
            }
        }
        
        private void Login()
        {
            try
            {
                if (_dataContext == null)
                    return;
                string[] properties = { "UserName", "Password" };
                if (!ValidateData(properties))
                    return;
                string pass =  Utils.CreateMD5Hash(Password);
                User user = _dataContext.Users.FirstOrDefault(u => u.UserName == UserName && u.Password == pass);
                if (user != null)
                {
                    ViewModelBase.CurrentUser = user;
                    Mediator.NotifyColleagues<User>(Messages.UserChanged, user);
                    Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
                }
                else
                {
                    ErrorMsg = Properties.Resources.String_UserNotMatch;
                    base.OnPropertyChanged("ErrorMsg");
                }
                
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }

        }
        
        private bool CanLogin()
        {
            return !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password);
        }

        public string UserName { get; set; }
        public string Password { get; set; }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                switch (property)
                {
                    case "UserName":
                        if (string.IsNullOrEmpty(this.UserName))
                            msg = Resources.ValidationLoginUserName_Error;
                        break;
                    case "Password":
                        if (string.IsNullOrEmpty(this.Password))
                            msg = Resources.ValidationLoginPassword_Error;
                        break;
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members

    }
}
